<?php
	/***************************************************************************
	* File    : Selectpayment.php
	* Version : $Id: SelectPayment.php,v 1.22 2009/08/18 11:41:28 thomas Exp $
	* Author  : wolfgang.schaefer@mPAY24.com, thomas.langer@mPAY24.com
	***************************************************************************/
	
	include_once("Item.php");
	include_once("Address.php");
	include_once("Style.php");

	class SelectPayment {
	
		// constructor
		function SelectPayment($url) {
			$etp_url = "https://";
		   if($url != "www")
			   $etp_url .= "test";
			else
			   $etp_url .= "www";
			$etp_url .= ".mPAY24.com/app/bin/etpv5";
			$this->etp_url = $etp_url;
		}
		
		// set-functions for POST to mPAY24
		function setMerchantID($id) {
			if(is_numeric($id)) {
			   $this->merchantid = urlencode($id);
			}
		}
		
		function setMerchantPass($pass){
		    $this->merchantpass = (string)$pass;
		}
		
		function setTID($tid) {
			if($tid == "") {
				$ip= (int) str_replace(".", "", $_SERVER['REMOTE_ADDR']);
				$ts= microtime();
				$rand = mt_rand(0, 100000);
				$seed = (string) $ip * $ts * $rand;
				$tid  = md5($seed);
			}
			if (strlen($tid) <= 32) {
				$this->tid = urlencode($tid);
			}
		}
		
		// public
		function setTemplateSet($set) {
			$this->templateset = "WEB";
		}
		
		// public
		function setLanguage($lang) {
			if(strlen($lang) == 2){
				$this->language = $lang;
			}
		}
		
		// public
		function setCssName($name) {
			$this->cssname = $name;
		}
		
		// public
		function setUserField($field) {
			if(strlen($field) <= 1000) {
				$this->userfield = $field;
			}
		}
		
		// public
		function setDescription($description) {
			if(strlen($description) <= 1000) {
				$this->description = $description;
			} else {
				$this->description = substr($description, 0, 1000);
			}
		}
		
		// public
		function addShippingCosts($header_value) {
			array_push($this->shippingcosts, $header_value);
		}
		
		function addTax($tax_array) {
			array_push($this->tax, $tax_array);
		}
		
		// public
		function setTotalPrice($price) {
				$this->total_price = $price;
		}
		
		// public
		function setCurrency($cur) {
			$len = strlen($cur);
			if($len == 3){
				$this->currency = $cur;
			}else{
				print "ERROR: wrong currency format specified!";
				exit;
			}
		}
		
		function addCustomer($details) {
			$this->customer_details = $details;
		}
		
		// public
		function addItem($item) {
			array_push($this->itemlist, $item);
		}
		
		function setPaymentEnab($show) {
			if($show) {
				$this->ptenable = "true";
			}
			elseif(!($show)) {
				$this->ptenable = "false";
			}
		}
		
		// public
		function addPaymentType($type_brand) {
			array_push($this->paymenttypes, $type_brand);
		}
		
		function setStyle($style) {
			$this->style = $style;
		}
		
		// public
		function setBillingAddress($addr) {
			$this->billing_addr = $addr;
		}
		
		// public
		function setXmlDir($path) {
			$this->xmldir = $path;
		}
		
		function setGetdataURL($url){
			$this->getdata_url = $url;
		}
		
		// public
		function setErrorURL($url){
			$this->error_url = $this->xmlentities($url);
		}
		
		function setSuccessURL($url){
			$this->success_url = $this->xmlentities($url);
		}
		
		function setConfirmURL($url){
			$this->confirm_url = $this->xmlentities($url);
		}
		
		// private
		function xmlentities($string, $quote_style=ENT_QUOTES) {
			static $trans;
			if (!isset($trans)) {
				$trans = get_html_translation_table(HTML_ENTITIES, $quote_style);
				foreach ($trans as $key => $value)
				$trans[$key] = '&#'.ord($key).';';
				// dont translate the '&' in case it is part of &xxx;
				$trans[chr(38)] = '&';
			}
			// after the initial translation, _do_ map standalone '&' into '&#38;'
			return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&#38;" , strtr($string, $trans));
		}
		
		// public
		function createXmlFile() {
			$type = undef;
			$brand= undef;
			if(phpversion() >= 5) {
			#if(false) {
				$doc = new DOMDocument("1.0", "UTF-8");
				$doc->formatOutput = true;
				$xmlOrder = $doc->createElement('Order');
				$xmlOrder = $doc->appendChild($xmlOrder);
				if ($this->style->getLogoStyle())
					$xmlOrder->setAttribute('LogoStyle', $this->style->getLogoStyle());
				if ($this->style->getPageHeaderStyle())
					$xmlOrder->setAttribute('PageHeaderStyle', $this->style->getPageHeaderStyle());
				if ($this->style->getPageCaptionStyle())
					$xmlOrder->setAttribute('PageCaptionStyle', $this->style->getPageCaptionStyle());
				if ($this->style->getPageStyle())
					$xmlOrder->setAttribute('PageStyle', $this->style->getPageStyle());
				if ($this->style->getFooterStyle())
					$xmlOrder->setAttribute('FooterStyle', $this->style->getFooterStyle());
				if ($this->style->getWholePageStyle())
					$xmlOrder->setAttribute("Style",$this->style->getWholePageStyle());
				if ($this->style->getInputFieldsStyle())
					$xmlOrder->setAttribute("InputFieldsStyle",$this->style->getInputFieldsStyle());
				
				$xmlUserField = $doc->createElement('UserField', $this->userfield);
				$xmlUserField = $xmlOrder->appendChild($xmlUserField);
				
				$xmlTid = $doc->createElement('Tid', $this->tid);
				$xmlTid = $xmlOrder->appendChild($xmlTid);
				
				$xmlTemplateSet = $doc->createElement('TemplateSet', $this->templateset);
				$xmlTemplateSet = $xmlOrder->appendChild($xmlTemplateSet);
				$xmlTemplateSet->setAttribute('Language', $this->language);
				
				$xmlPaymentTypes = $doc->createElement('PaymentTypes');
				if(!empty($this->paymenttypes)) {
					$xmlPaymentTypes = $xmlOrder->appendChild($xmlPaymentTypes);
					if($this->ptenable)
						$xmlPaymentTypes->setAttribute('Enable', $this->ptenable);
					for($i = 0; $i < count($this->paymenttypes); $i++) {
						$xmlPayment = $doc->createElement('Payment');
						$xmlPayment = $xmlPaymentTypes->appendChild($xmlPayment);
						if(strpos($this->paymenttypes[$i], ",") == false) {
							$xmlPayment->setAttribute('Type', $this->paymenttypes[$i]);
						} else {
							list($type, $brand) = split(",", $this->paymenttypes[$i]);
							$xmlPayment->setAttribute('Type', $type);
							$xmlPayment->setAttribute('Brand', $brand);
						}
					}
				}
				
				$xmlShoppingCart = $doc->createElement('ShoppingCart');
				$xmlShoppingCart = $xmlOrder->appendChild($xmlShoppingCart);
				if ($this->style->getSCStyle())
					$xmlShoppingCart->setAttribute('Style', $this->style->getSCStyle());
				if ($this->style->getSCHeader())
					$xmlShoppingCart->setAttribute('Header', $this->style->getSCHeader());
				if ($this->style->getSCHeaderStyle())
					$xmlShoppingCart->setAttribute('HeaderStyle', $this->style->getSCHeaderStyle());
				if ($this->style->getSCCaptionStyle())
					$xmlShoppingCart->setAttribute('CaptionStyle', $this->style->getSCCaptionStyle());
				if ($this->style->getSCNumberStyle())
					$xmlShoppingCart->setAttribute('NumberStyle', $this->style->getSCNumberStyle());
				if ($this->style->getSCProductNrStyle())
					$xmlShoppingCart->setAttribute('ProductNrStyle', $this->style->getSCProductNrStyle());
				if ($this->style->getSCDescriptionStyle())
					$xmlShoppingCart->setAttribute('DescriptionStyle', $this->style->getSCDescriptionStyle());
				if ($this->style->getSCPackageStyle())
					$xmlShoppingCart->setAttribute('PackageStyle', $this->style->getSCPackageStyle());
				if ($this->style->getSCQuantityStyle())
					$xmlShoppingCart->setAttribute('QuantityStyle', $this->style->getSCQuantityStyle());
				if ($this->style->getSCItemPriceStyle())
					$xmlShoppingCart->setAttribute('ItemPriceStyle', $this->style->getSCItemPriceStyle());
				if ($this->style->getSCPriceStyle())
					$xmlShoppingCart->setAttribute('PriceStyle', $this->style->getSCPriceStyle());
				$xmlDescription  = $doc->createElement('Description', $this->description);
				$xmlDescription  = $xmlShoppingCart->appendChild($xmlDescription);
				
				for($i = 0; $i < count($this->itemlist); $i++) {
					$xmlItem = $doc->createElement('Item');
					$xmlItem = $xmlShoppingCart->appendChild($xmlItem);
					if($this->itemlist[$i]->getNumber()){
						$xmlNumber  = $doc->createElement('Number', $this->xmlentities($this->itemlist[$i]->getNumber()));
						$xmlNumber  = $xmlItem->appendChild($xmlNumber);
					}
					if($this->itemlist[$i]->getProductNr()){
						$xmlProductNumber = $doc->createElement('ProductNr', $this->xmlentities($this->itemlist[$i]->getProductNr()));
						$xmlProductNumber = $xmlItem->appendChild($xmlProductNumber);
					}
					if($this->itemlist[$i]->getDescription()){
						$xmlDescription= $doc->createElement('Description', $this->xmlentities($this->itemlist[$i]->getDescription()));
						$xmlDescription= $xmlItem->appendChild($xmlDescription);
					}
					if($this->itemlist[$i]->getPackage()){
						$xmlPackage = $doc->createElement('Package', $this->xmlentities($this->itemlist[$i]->getPackage()));
						$xmlPackage = $xmlItem->appendChild($xmlPackage);
					}
					if($this->itemlist[$i]->getQuantity()){
						$xmlQuantity= $doc->createElement('Quantity', $this->xmlentities($this->itemlist[$i]->getQuantity()));
						$xmlQuantity= $xmlItem->appendChild($xmlQuantity);
					}
					if($this->itemlist[$i]->getItemPrice()){
						$xmlItemPrice  = $doc->createElement('ItemPrice', $this->xmlentities(number_format($this->itemlist[$i]->getItemPrice(),2,'.','')));
						$xmlItemPrice  = $xmlItem->appendChild($xmlItemPrice);
					}
					if($this->itemlist[$i]->getPrice()){
						$xmlPrice= $doc->createElement('Price', $this->xmlentities(number_format($this->itemlist[$i]->getPrice(),2,'.','')));
						$xmlPrice= $xmlItem->appendChild($xmlPrice);
					}
					if(($i % 2) && $this->style->getSCItemEvenStyle()) {
						if($xmlNumber)
							$xmlNumber->setAttribute('Style', $this->style->getSCItemEvenStyle());
						if($xmlProductNumber)
							$xmlProductNumber->setAttribute('Style', $this->style->getSCItemEvenStyle());
						if($xmlDescription)
							$xmlDescription->setAttribute('Style', $this->style->getSCItemEvenStyle());
						if($xmlPackage)
							$xmlPackage->setAttribute('Style', $this->style->getSCItemEvenStyle());
						if($xmlQuantity)
							$xmlQuantity->setAttribute('Style', $this->style->getSCItemEvenStyle());
						if($xmlItemPrice)
							$xmlItemPrice->setAttribute('Style', $this->style->getSCItemEvenStyle());
						if($xmlPrice)
							$xmlPrice->setAttribute('Style', $this->style->getSCItemEvenStyle());
					} elseif(!($i % 2) && $this->style->getSCItemOddStyle()) {
						if($xmlNumber)
							$xmlNumber->setAttribute('Style', $this->style->getSCItemOddStyle());
						if($xmlProductNumber)
							$xmlProductNumber->setAttribute('Style', $this->style->getSCItemOddStyle());
						if($xmlDescription)
							$xmlDescription->setAttribute('Style', $this->style->getSCItemOddStyle());
						if($xmlPackage)
							$xmlPackage->setAttribute('Style', $this->style->getSCItemOddStyle());
						if($xmlQuantity)
							$xmlQuantity->setAttribute('Style', $this->style->getSCItemOddStyle());
						if($xmlItemPrice)
							$xmlItemPrice->setAttribute('Style', $this->style->getSCItemOddStyle());
						if($xmlPrice)
							$xmlPrice->setAttribute('Style', $this->style->getSCItemOddStyle());
					}
				}
				
				for($i = 0; $i < count($this->shippingcosts); $i++) {
					if(strpos($this->shippingcosts[$i], ",") == false) {
						$xmlShippingCosts = $doc->createElement('ShippingCosts', number_format($this->shippingcosts[$i],2,'.',''));
						$xmlShippingCosts = $xmlShoppingCart->appendChild($xmlShippingCosts);
					} else {
						list($header, $value) = split(",", $this->shippingcosts[$i]);
						$xmlShippingCosts = $doc->createElement('ShippingCosts', number_format($value,2,'.',''));
						$xmlShippingCosts = $xmlShoppingCart->appendChild($xmlShippingCosts);
						$xmlShippingCosts->setAttribute('Header', $this->xmlentities($header));
					}
					if($this->style->getShippingCostsHStyle())
						$xmlShippingCosts->setAttribute('HeaderStyle', $this->style->getShippingCostsHStyle());
					if($this->style->getShippingCostsStyle())
						$xmlShippingCosts->setAttribute('Style', $this->style->getShippingCostsStyle());
				}
				
				for($i = 0; $i < count($this->tax); $i++) {
					if(strpos($this->tax[$i], ",") == false) {
						$xmlTax = $doc->createElement('Tax', number_format($this->tax[$i],2,'.',''));
						$xmlTax = $xmlShoppingCart->appendChild($xmlTax);
					} else {
						list($value1, $value2, $value3) = split(",", $this->tax[$i]);
						if($value3) {
							$xmlTax = $doc->createElement('Tax', number_format($value3,2,'.',''));
							$xmlTax = $xmlShoppingCart->appendChild($xmlTax);
							$xmlTax->setAttribute('Percent', $value1);
							$xmlTax->setAttribute('Header', $value2);
						}
						elseif(is_numeric($value1) && $value3 == false) {
							$xmlTax = $doc->createElement('Tax', number_format($value2,2,'.',''));
							$xmlTax = $xmlShoppingCart->appendChild($xmlTax);
							$xmlTax->setAttribute('Percent', $value1);
						} else {
							$xmlTax = $doc->createElement('Tax', number_format($value2,2,'.',''));
							$xmlTax = $xmlShoppingCart->appendChild($xmlTax);
							$xmlTax->setAttribute('Header', $value1);
						}
					}
					if($this->style->getTaxHStyle())
						$xmlTax->setAttribute('HeaderStyle', $this->style->getTaxHStyle());
					if($this->style->getTaxStyle())
						$xmlTax->setAttribute('Style', $this->style->getTaxStyle());
				}

				$xmlPrice = $doc->createElement('Price', $this->total_price);
				$xmlPrice = $xmlOrder->appendChild($xmlPrice);
				if ($this->style->getPRHeader())
					$xmlPrice->setAttribute('Header', $this->style->getPRHeader());
				if ($this->style->getPRHeaderStyle())
					$xmlPrice->setAttribute('HeaderStyle', $this->style->getPRHeaderStyle());
				if ($this->style->getPRStyle())
					$xmlPrice->setAttribute('Style',  $this->style->getPRStyle());
				
				if($this->currency){
					$xmlCurrency= $doc->createElement('Currency', $this->currency);
					$xmlCurrency= $xmlOrder->appendChild($xmlCurrency);
					$xmlCurrency->setAttribute('Display', $this->currency);
				}
				
				if($this->customer_details) {
					list($id, $set)= split(",", $this->customer_details);
					$xmlCustomer= $doc->createElement('Customer');
					$xmlCustomer= $xmlOrder->appendChild($xmlCustomer);
					$xmlCustomer->setAttribute('Id', $id);
					$xmlCustomer->setAttribute('UseProfile', $set);
				}
				
				$xmlBillingAddr= $doc->createElement('BillingAddr');
				$xmlBillingAddr= $xmlOrder->appendChild($xmlBillingAddr);
				$xmlBillingAddr->setAttribute('Mode', 'ReadWrite');
				$xmlFirstName  = $doc->createElement('FirstName', utf8_encode($this->billing_addr->getFirstName()));
				$xmlFirstName  = $xmlBillingAddr->appendChild($xmlFirstName);
				$xmlLastName= $doc->createElement('LastName', utf8_encode($this->billing_addr->getLastName()));
				$xmlLastName= $xmlBillingAddr->appendChild($xmlLastName);
				$xmlStreet  = $doc->createElement('Street', utf8_encode($this->billing_addr->getStreet()));
				$xmlStreet  = $xmlBillingAddr->appendChild($xmlStreet);
				$xmlZip  = $doc->createElement('Zip', utf8_encode($this->billing_addr->getZip()));
				$xmlZip  = $xmlBillingAddr->appendChild($xmlZip);
				$xmlCity = $doc->createElement('City', utf8_encode($this->billing_addr->getCity()));
				$xmlCity = $xmlBillingAddr->appendChild($xmlCity);
				$xmlCountry = $doc->createElement('Country', utf8_encode($this->billing_addr->getCountry()));
				$xmlCountry = $xmlBillingAddr->appendChild($xmlCountry);
				$xmlEmail= $doc->createElement('Email', utf8_encode($this->billing_addr->getEmail()));
				$xmlEmail= $xmlBillingAddr->appendChild($xmlEmail);
				
				$xmlURL = $doc->createElement('URL');
				$xmlURL = $xmlOrder->appendChild($xmlURL);
				$xmlSuc = $doc->createElement("Success",$this->xmlentities($this->success_url));
				$xmlSuc = $xmlURL->appendChild($xmlSuc);
				$xmlErr = $doc->createElement("Error",$this->xmlentities($this->error_url));
				$xmlErr = $xmlURL->appendChild($xmlErr);
				$xmlCon = $doc->createElement("Confirmation",$this->xmlentities($this->confirm_url));
				$xmlCon = $xmlURL->appendChild($xmlCon);
				
				$this->xmlfile  = $doc->saveXML();
			} else {
				$this->xmlfile = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" . 
				"<Order";
				if ($this->style->getLogoStyle())
					$this->xmlfile .= " LogoStyle=\"" . $this->style->getLogoStyle() . "\"";
				if ($this->style->getPageHeaderStyle())
					$this->xmlfile .= " PageHeaderStyle=\"" . $this->style->getPageHeaderStyle() . "\"";
				if ($this->style->getPageCaptionStyle())
					$this->xmlfile .= " PageCaptionStyle=\"" . $this->style->getPageCaptionStyle() . "\"";
				if ($this->style->getPageStyle())
					$this->xmlfile .= " PageStyle=\"" . $this->style->getPageStyle() . "\"";
				if ($this->style->getFooterStyle())
					$this->xmlfile .= " FooterStyle=\"" . $this->style->getFooterStyle() . "\"";
				if ($this->style->getWholePageStyle())
					$this->xmlfile .= " Style=\"" . $this->style->getWholePageStyle() . "\"";
				if ($this->style->getInputFieldsStyle())
					$this->xmlfile .= " InputFieldsStyle=\"" . $this->style->getInputFieldsStyle() . "\"";
				$this->xmlfile .= ">\n";
				
				if (!empty($this->userfield))
					$this->xmlfile .= " <UserField>" . $this->userfield . "</UserField>\n";
				$this->xmlfile .= " <Tid>" . $this->tid . "</Tid>\n";
				
				if (!empty($this->TemplateSet) || !empty($this->language)) {
					$this->xmlfile .= " <TemplateSet";
					if(!empty($this->language))
						$this->xmlfile .= " Language=\"" . $this->language . "\"";
					$this->xmlfile .= ">" . $this->TemplateSet . "</TemplateSet>\n";
				}
				
				if(!empty($this->paymenttypes)) {
					$this->xmlfile .= " <PaymentTypes";
					if ($this->ptenable)
						$this->xmlfile .= " Enable=\"" . $this->ptenable . "\"";
					$this->xmlfile .= ">\n";
					for ($i = 0; $i < count($this->paymenttypes); $i++) {
						$this->xmlfile .= "  <Payment";
						if(strpos($this->paymenttypes[$i], ",") == false) {
							$this->xmlfile .= " Type=\"" . $this->paymenttypes[$i] . "\"";
						} else {
							list($type, $brand) = split(",", $this->paymenttypes[$i]);
							$this->xmlfile .= " Type=\"" . $type . "\"" .
							" Brand=\"" . $brand . "\"";
						}
						$this->xmlfile .= "/>\n";
					}
					$this->xmlfile .= " </PaymentTypes>\n";
				}
				
				if ($this->style->getSCStyle()||
					$this->style->getSCHeader()||
					$this->style->getSCHeaderStyle() ||
					$this->style->getSCCaptionStyle()||
					$this->style->getSCNumberStyle() ||
					$this->style->getSCProductNrStyle() ||
					$this->style->getSCDescriptionStyle()  ||
					$this->style->getSCPackageStyle()||
					$this->style->getSCQuantityStyle()  ||
					$this->style->getSCItemPriceStyle() ||
					$this->style->getSCPriceStyle()  ||
					!empty($this->description) ||
					!empty($this->itemlist))
				{
					$this->xmlfile .= " <ShoppingCart";
					if ($this->style->getSCStyle())
						$this->xmlfile .= " Style=\"" . $this->style->getSCStyle() . "\"";
					if ($this->style->getSCHeader())
						$this->xmlfile .= " Header=\"" . $this->style->getSCHeader() . "\"";
					if ($this->style->getSCHeaderStyle())
						$this->xmlfile .= " HeaderStyle=\"" . $this->style->getSCHeaderStyle() . "\"";
					if ($this->style->getSCCaptionStyle())
						$this->xmlfile .= " CaptionStyle=\"" . $this->style->getSCCaptionStyle() . "\"";
					if ($this->style->getSCNumberStyle())
						$this->xmlfile .= " NumberStyle=\"" . $this->style->getSCNumberStyle() . "\"";
					if ($this->style->getSCProductNrStyle())
						$this->xmlfile .= " ProductNrStyle=\"" . $this->style->getSCProductNrStyle() . "\"";
					if ($this->style->getSCDescriptionStyle())
						$this->xmlfile .= " DescriptionStyle=\"" . $this->style->getSCDescriptionStyle() . "\"";
					if ($this->style->getSCPackageStyle())
						$this->xmlfile .= " PackageStyle=\"" . $this->style->getSCPackageStyle() . "\"";
					if ($this->style->getSCQuantityStyle())
						$this->xmlfile .= " QuantityStyle=\"" . $this->style->getSCQuantityStyle() . "\"";
					if ($this->style->getSCItemPriceStyle())
						$this->xmlfile .= " ItemPriceStyle=\"" . $this->style->getSCItemPriceStyle() . "\"";
					if ($this->style->getSCPriceStyle())
						$this->xmlfile .= " PriceStyle=\"" . $this->style->getSCPriceStyle() . "\"";
					$this->xmlfile .= ">\n";
					if (!empty($this->description))
						$this->xmlfile .= "  <Description>" . $this->description . "</Description>\n";
					for($i = 0; $i < count($this->itemlist); $i++) {
						$this->xmlfile .= "  <Item>\n";
						if ($this->itemlist[$i]->getNumber()) {
							$this->xmlfile .= "<Number";
							if (($i % 2) && $this->style->getSCItemEvenStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemEvenStyle() . "\"";
							elseif (!($i % 2) && $this->style->getSCItemOddStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemOddStyle() . "\"";
							$this->xmlfile .= ">" . $this->itemlist[$i]->getNumber() . "</Number>\n";
						}
						if ($this->itemlist[$i]->getProductNr()) {
							$this->xmlfile .= "<ProductNr";
							if (($i % 2) && $this->style->getSCItemEvenStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemEvenStyle() . "\"";
							elseif (!($i % 2) && $this->style->getSCItemOddStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemOddStyle() . "\"";
							$this->xmlfile .= ">" . $this->itemlist[$i]->getProductNr() . "</ProductNr>\n";
						}
						if ($this->itemlist[$i]->getDescription()) {
							$this->xmlfile .= "<Description";
							if (($i % 2) && $this->style->getSCItemEvenStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemEvenStyle() . "\"";
							elseif (!($i % 2) && $this->style->getSCItemOddStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemOddStyle() . "\"";
							$this->xmlfile .= ">" . $this->itemlist[$i]->getDescription() . "</Description>\n";
						}
						if ($this->itemlist[$i]->getPackage()) {
							$this->xmlfile .= "<Package";
							if (($i % 2) && $this->style->getSCItemEvenStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemEvenStyle() . "\"";
							elseif (!($i % 2) && $this->style->getSCItemOddStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemOddStyle() . "\"";
							$this->xmlfile .= ">" . $this->itemlist[$i]->getPackage() . "</Package>\n";
						}
						if ($this->itemlist[$i]->getQuantity()) {
							$this->xmlfile .= "<Quantity";
							if (($i % 2) && $this->style->getSCItemEvenStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemEvenStyle() . "\"";
							elseif (!($i % 2) && $this->style->getSCItemOddStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemOddStyle() . "\"";
							$this->xmlfile .= ">" . $this->itemlist[$i]->getQuantity() . "</Quantity>\n";
						}
						if ($this->itemlist[$i]->getItemPrice()) {
							$this->xmlfile .= "<ItemPrice";
							if (($i % 2) && $this->style->getSCItemEvenStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemEvenStyle() . "\"";
							elseif (!($i % 2) && $this->style->getSCItemOddStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemOddStyle() . "\"";
							$this->xmlfile .= ">" . number_format($this->itemlist[$i]->getItemPrice(),2,'.','') . "</ItemPrice>\n";
						}
						if ($this->itemlist[$i]->getPrice()) {
							$this->xmlfile .= "<Price";
							if (($i % 2) && $this->style->getSCItemEvenStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemEvenStyle() . "\"";
							elseif (!($i % 2) && $this->style->getSCItemOddStyle())
								$this->xmlfile .= " Style=\"" . $this->style->getSCItemOddStyle() . "\"";
							$this->xmlfile .= ">" . number_format($this->itemlist[$i]->getPrice(),2,'.','') . "</Price>\n";
						}
						$this->xmlfile .= "  </Item>\n";
					}
					for($i = 0; $i < count($this->shippingcosts); $i++) {
						$this->xmlfile .= "  <ShippingCosts";
						if($this->style->getShippingCostsHStyle())
							$this->xmlfile .= " HeaderStyle=\"" . $this->style->getShippingCostsHStyle() . "\"";
						if($this->style->getShippingCostsStyle())
							$this->xmlfile .= " Style=\"" . $this->style->getShippingCostsStyle() . "\"";
						if(strpos($this->shippingcosts[$i], ",") == false)
							$this->xmlfile .= ">" . number_format($this->shippingcosts[$i],2,'.','') . "</ShippingCosts>\n";
						else {
							list($header, $value) = split(",", $this->shippingcosts[$i]);
							$this->xmlfile .= " Header=\"" . $header . "\">" . number_format($value,2,'.','') . "</ShippingCosts>\n";
						}
					}
					for($i = 0; $i < count($this->tax); $i++) {
						$this->xmlfile .= "  <Tax";
						if($this->style->getTaxHStyle())
							$this->xmlfile .= " HeaderStyle=\"" . $this->style->getTaxHStyle() . "\"";
						if($this->style->getTaxStyle())
							$this->xmlfile .= " Style=\"" . $this->style->getTaxStyle() . "\"";
						if(strpos($this->tax[$i], ",") == false)
							$this->xmlfile .= ">" . number_format($this->tax[$i],2,'.','') . "</Tax>\n";
						else {
							list($value1, $value2, $value3) = split(",", $this->tax[$i]);
							if($value3)
								$this->xmlfile .= " Percent=\"" . $value1 . "\" Header=\"" . $value2 . "\">" . number_format($value3,2,'.','') . "</Tax>\n";
							elseif(is_numeric($value1) && $value3 == false)
								$this->xmlfile .= " Percent=\"" . $value1 . "\">" . number_format($value2,2,'.','') . "</Tax>\n";
							else
								$this->xmlfile .= " Header=\"" . $value1 . "\">" . number_format($value2,2,'.','') . "</Tax>\n";
						}
					}
					$this->xmlfile .= " </ShoppingCart>\n";
				}
				$this->xmlfile .= " <Price";
				if ($this->style->getPRHeader())
					$this->xmlfile .= " Header=\"" . $this->style->getPRHeader() . "\"";
				if ($this->style->getPRHeaderStyle())
					$this->xmlfile .= " HeaderStyle=\"" . $this->style->getPRHeaderStyle() . "\"";
				if ($this->style->getPRStyle())
					$this->xmlfile .= " Style=\"" . $this->style->getPRStyle() . "\"";
				$this->xmlfile .= ">" . number_format($this->total_price,2,'.','') . "</Price>\n";
				if($this->currency)
					$this->xmlfile .= " <Currency>" . $this->currency . "</Currency>\n";
				if($this->customer_details) {
					list($id, $set)= split(",", $this->customer_details);
					$this->xmlfile .= " <Customer Id=\"" . $id . "\" UseProfile=\"" . $set . "\"/>\n";
				}
				if($this->billing_addr) {
					$this->xmlfile .= " <BillingAddr Mode=\"ReadWrite\">\n";
					$this->xmlfile .= "  <FirstName>" . $this->xmlentities($this->billing_addr->getFirstName()) . "</FirstName>\n";
					$this->xmlfile .= "  <LastName>"  . $this->xmlentities($this->billing_addr->getLastName())  . "</LastName>\n";
					$this->xmlfile .= "  <Street>" . $this->xmlentities($this->billing_addr->getStreet()) . "</Street>\n";
					$this->xmlfile .= "  <Zip>" . $this->xmlentities($this->billing_addr->getZip()) . "</Zip>\n";
					$this->xmlfile .= "  <City>". $this->xmlentities($this->billing_addr->getCity()). "</City>\n";
					$this->xmlfile .= "  <Country>". $this->xmlentities($this->billing_addr->getCountry()). "</Country>\n";
					  if($this->billing_addr->getEmail())
					  $this->xmlfile .= "  <Email>" . $this->xmlentities($this->billing_addr->getEmail()) . "</Email>\n";
					$this->xmlfile .= " </BillingAddr>\n";
				}
				
				if($this->success_url || $this->error_url || $this->confirm_url){
					$this->xmlfile .= " <URL>\n";
					if($this->success_url)
						$this->xmlfile .= "  <Success>" . $this->xmlentities($this->success_url) . "</Success>\n";
					if($this->error_url)
						$this->xmlfile .= "  <Error>" . $this->xmlentities($this->error_url) . "</Error>\n";
					if($this->confirm_url)
						$this->xmlfile .= "  <Confirmation>" . $this->xmlentities($this->confirm_url) . "</Confirmation>\n";
					$this->xmlfile .= " </URL>\n";
				}
				
				$this->xmlfile .= "</Order>";
			}
		}
		// end function createXmlFile()
		
		// public
		function getXmlFile() {
			return $this->xmlfile;
		}
		
		// public
		function saveXmlFile() {
			$filename = $this->xmldir . "/" . $this->tid . ".xml";
			if(! $fh = fopen($filename, 'w')) {
				echo "Cannot open file ($filename)";
				exit(1);
			}
			if(fwrite($fh, $this->xmlfile) == FALSE) {
				echo "Cannot write to file ($filename)";
				exit(1);
			}
			fclose($fh);
		}
		
		// private
		function isValid() {
			$status = true;
			// check for mandatory parameters
			if($this->merchantid == "") {
				print("Error : No/Wrong MERCHANTID specified !<br>\n");
				$status = false;
			}
			if($this->tid == "") {
				print("Error : No/Wrong TID specified !<br>\n");
				$status = false;
			}
			
			return $status;
		}

         // private
         function buildRequest() {
            $this->request = "OPERATION="   . urlencode($this->operation)  . "&" .
               "MERCHANTID="  . urlencode($this->merchantid) . "&" .
               "TID="         . urlencode($this->tid)        . "&" .
               "MDXI="        . urlencode($this->xmlfile)    . "&" .
               "GETDATA_URL=" . urlencode($this->getdata_url);
         }
		
         // public
         function send() {
            if($this->isValid()) {
               
               /*
                * if SOAP is installed, try to perform a SOAP request
                * else, try to use cURL
                * else, throw an error message that neither of those are installed
                */
               
               if(class_exists("SoapClient")){
               #if(false){
                  $client = new SoapClient('https://www.mPAY24.com/soap/etp/1.4/ETP.wsdl',array(   // TODO: Move authentication information to a more secure area,
                                                                                                   // e.g. an authentication file outside the web servers
                                                                                                   // document root.
                                                                                                   'login'        => 'u' . $this->merchantid,
                                                                                                   'password'     => $this->merchantpass,
                                                                                                   
                                                                                                   // TODO: Move proxy information to a more secure area,
                                                                                                   // e.g. a configuration file outside the web servers
                                                                                                   // document root.
                                                                                                   #'proxy_host'  => 'aaa.bbb.ccc.ddd',
                                                                                                   #'proxy_port'  => 'port',
                                                                                                   #...
                                                                                                   
                                                                                                   // exceptions => FALSE to cleanly catch errors
                                                                                                   'exceptions'   => FALSE));
                  
                  // if we are on test system, override the Location set within the WSDL
                  if($this->etp_url == "https://test.mPAY24.com/app/bin/etpv5")
                     $client->__setLocation('https://test.mPAY24.com/app/bin/etpproxy_v14');
                     
                  // perform the request
                  $this->response = $client->SelectPayment(array( 'mdxi'         => $this->xmlfile,
                                                                  'merchantID'   => $this->merchantid,
                                                                  'getDataURL'   => '',
                                                                  'tid'          => ''));
               }elseif(function_exists("curl_init")){
               #}if(false){
                  
                  // build Request
                  $this->buildRequest();
                  
                  // send request;
                  $ch = curl_init($this->etp_url);
                  curl_setopt($ch, CURLOPT_HEADER, 0);
                  curl_setopt($ch, CURLOPT_POST, 1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                  
                  // TODO: Move proxy information to a more secure area,
                  // e.g. a configuration file outside the web servers
                  // document root.
                  #curl_setopt($ch, CURLOPT_PROXY, 'aaa.bbb.ccc.ddd:port');
                  #...
                  
                  // NOT FOR PRODUCTION USE
                  #curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
                  
                  // uncomment the following line in case you experience trouble regarding cURL SSL certificate errors
                  // Plese download cacert.pem from http://curl.haxx.se/docs/caextract.html
                  #curl_setopt($ch, CURLOPT_CAINFO, "./cacert.pem");
                  
                  // get response
                  $this->response = curl_exec($ch);
                  
                  // catch errors
                  $ch_error = curl_error($ch);
                  if(empty($this->response) || !empty($ch_error)){
                     $this->response = "STATUS=ERROR&RETURNCODE=cURL+-+" . urlencode(curl_error($ch));
                  }
                  curl_close($ch);
               }else{
                  $this->response = "STATUS=ERROR&RETURNCODE=Your+PHP+installation+does+not+meet+either+of+the+following+requirements%3A%3Cul%3E%3Cli%3E%3Ca+href%3D%27http%3A%2F%2Fwww.php.net%2FcURL%27%3EcURL%3C%2Fa%3E+or%3C%2Fli%3E%3Cli%3E%3Ca+href%3D%27http%3A%2F%2Fwww.php.net%2FSOAP%27%3ESOAP%3C%2Fa%3E%3C%2Fli%3E%3C%2Ful%3E";
               }
            }
         }
		
         // public
         function parseResponse() {
            
            // if SOAP was used, response is returned as an object
            if(is_object($this->response)){
               if(is_soap_fault($this->response)){
                  $this->STATUS = "ERROR";
                  $this->RETURNCODE = 'SOAP - ' . $this->response->faultstring . '&nbsp;(' . $this->response->faultcode . ')';
               }else{
                  $this->STATUS     = strtoupper($this->response->out->status);
                  $this->RETURNCODE = strtoupper($this->response->out->returnCode);
                  $this->LOCATION   = $this->response->out->location;
               }
               
            // cURL returns a urlencoded text/plain response
            }else{
               $parameters = split("&", $this->response);
               
               for($i=0; $i<count($parameters);$i++) {
                  list($html_resp[$i]['name'],$html_resp[$i]['value'])  = split("=",$parameters[$i]);
                  if(strtoupper($html_resp[$i]['name']) == 'STATUS')     $this->STATUS     = urldecode(strtoupper($html_resp[$i]['value']));
                  if(strtoupper($html_resp[$i]['name']) == 'RETURNCODE') $this->RETURNCODE = urldecode(strtoupper($html_resp[$i]['value']));
                  if(strtoupper($html_resp[$i]['name']) == 'LOCATION')   $this->LOCATION   = urldecode($html_resp[$i]['value']);
               }
            }
         }
		
		function getResponse() {
			return $this->response;
		}
		
	  function getSTATUS(){
         return $this->STATUS;
      }
		
	  function getRETURNCODE(){
         return $this->RETURNCODE;
      }
		
		function getLOCATION(){
		   return $this->LOCATION;
		}		
		
		// member-variables
		var $etp_url;
		var $merchantid;
		var $tid;
		var $templateset;
		var $language = "DE";
		var $cssname;
		var $description;
		var $shippingcosts	= array();
		var $tax			= array();
		var $total_price;
		var $currency;
		var $customer_details;
		var $ptenable;
		var $itemlist		= array();
		var $paymenttypes	= array();
		var $style;					// Object
		var $billing_addr;			// Object
		var $decimal_point	= ".";	// set your preferred decimal point here
		var $xmldir;
		var $xmlfile;
		var $STATUS;
		var $operation		= "SELECTPAYMENT";
		var $request;
		var $RETURNCODE;
		var $LOCATION;
		var $mpaytid;
		var $error_url;
		var $response;
		var $success_url;
		var $confirm_url;
		var $getdata_url;
	}
?>
